<template>
  <div class="radarchat">
    <div id="statisticRadarchart" class="echart"></div>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { PieChart } from "echarts/charts";
import colorConst from "@/utils/custom-color.js";
echarts.use([PieChart]);
export default {
  name: "radarChartosta",
  props: {
    // 雷达图数据
    radarData: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    radarData: {
      handler(val) {
        if (val) {
          this.getChartsdata(this.radarData);
        }
      },
      deep: true,
      // immediate: true,
    },
  },
  beforeDestroy() {
    this.chartDes();
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },

    getChartsdata(radarData) {
      let legendName = [];
      radarData.map((item) => {
        legendName.push(item.name);
        return item;
      });
      let option = {
        color: colorConst.overviewPublicInstitutions,
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          top: "20%",
          left: "right",
          data: legendName,
        },
        series: [
          {
            name: "报警机制",
            type: "pie",
            minAngle: 10, //设置扇形的最小占比
            data: radarData,
          },
        ],
      };
      this.initChart("statisticRadarchart", option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.radarchat {
  .echart {
    width: 100%;
    height: 220px;
    padding-right: 5%;
  }
}
</style>
